package com.song.JavaThread_ch3_7;

import java.util.Date;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

public class Student implements Runnable {
	
	public Phaser phaser;

	public Student(Phaser phaser) {
		super();
		this.phaser = phaser;
	}



	@Override
	public void run() {
		System.out.printf(" %s ; has arrived to the exam  %s \n" ,Thread.currentThread().getName(), new Date());
		phaser.arriveAndAwaitAdvance();
		
		System.out.printf(" %s is going to first exam  %s \n",Thread.currentThread().getName() , new Date());
		doExercise1();
		System.out.printf(" %s is has done  to first exam  %s \n",Thread.currentThread().getName() , new Date());
		phaser.arriveAndAwaitAdvance();
		
		System.out.printf(" %s is going to second exam  %s \n",Thread.currentThread().getName() , new Date());
		doExercise2();
		System.out.printf(" %s is has done  to second exam  %s \n",Thread.currentThread().getName() , new Date());
		phaser.arriveAndAwaitAdvance();
		
		System.out.printf(" %s is going to third exam  %s \n",Thread.currentThread().getName() , new Date());
		doExercise3();
		System.out.printf(" %s is has done  to third exam  %s \n",Thread.currentThread().getName() , new Date());
		phaser.arriveAndAwaitAdvance();
	}



	private void doExercise3() {

		long duration = (long)(Math.random()*10);
		try {
			TimeUnit.SECONDS.sleep(duration);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}



	private void doExercise2() {
		long duration = (long)(Math.random()*10);
		try {
			TimeUnit.SECONDS.sleep(duration);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}



	private void doExercise1() {
		long duration = (long)(Math.random()*10);
		try {
			TimeUnit.SECONDS.sleep(duration);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
